#from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.views.generic.list import (BaseListView,
                                       MultipleObjectTemplateResponseMixin)
from django.views.generic.detail import (BaseDetailView,
                                         SingleObjectTemplateResponseMixin)
from django.views.generic import TemplateView

from malachite.responses import Template403Response
from malachite.models import Post, Tag
from malachite.mixins import (JSONPostsListsResponseMixin,
                              JSONPostDetailResponseMixin)
#from malachite.forms import ContactForm
from malachite.utils import get_http_accepted



class ContactFormView(TemplateView):
    template_name = "contact.html"
    success_url = 'thanks'

    # FIXME

#    def render_to_response(self, context, **response_kwargs):
#        if self.request.method == 'POST':
#            form_data = self.request.POST
#            context['form'] = ContactForm(form_data)
#            return HttpResponseRedirect(context['absolute_url'] + '/thanks/')
#        else:
#            context['form'] = ContactForm()
#            return super(ContactFormView, self).render_to_response(
#                context, **response_kwargs
#            )


class HybridPostsListView(JSONPostsListsResponseMixin,
                          MultipleObjectTemplateResponseMixin, BaseListView):
    template_name = "posts_list.html"

    def render_to_response(self, context, **response_kwargs):
        # Return the correct representation based on accepted encoding
        if get_http_accepted(self.request).endswith('json'):
            return JSONPostsListsResponseMixin.render_to_response(
                self, RequestContext(self.request, context)
            )
        else:
            return MultipleObjectTemplateResponseMixin.render_to_response(
                self, context, **response_kwargs
            )


class HybridPostsbyTagListView(HybridPostsListView):
    template_name = "posts_list.html"

    def get_queryset(self):
        tags = self.args[0].split(',')
        tag_ids = [tag.id for tag in Tag.objects.filter(name__in=tags)]
        return Post.objects.filter(tags__in=tag_ids).distinct()


class HybridPostDetailView(JSONPostDetailResponseMixin,
                           SingleObjectTemplateResponseMixin, BaseDetailView):
    template_name = "post_detail.html"

    def render_to_response(self, context, **response_kwargs):
        ctxt = RequestContext(self.request, context)
        # Return the correct representation based on accepted encoding
        if get_http_accepted(self.request).endswith('json'):
            return JSONPostDetailResponseMixin.render_to_response(
                self, ctxt
            )
        else:
            # Check if the user has the rights to access this resource
            if ctxt['post'].is_published or ctxt['is_logged_in']:
                return SingleObjectTemplateResponseMixin.render_to_response(
                    self, ctxt, **response_kwargs
                )
            else:
                # Return a 403 error
                return Template403Response(ctxt)


